package org.example.sample2.imp;

import org.example.sample2.api.CouponStrategy;
import org.example.sample2.dao.Coupon;

import java.math.BigDecimal;

public class UseCouponStrategy implements CouponStrategy {
    @Override
    public BigDecimal calculateTotal(Coupon coupon,BigDecimal totalPrice) {
        if (coupon!=null){
            if (coupon.getMinusNum()!=null&&coupon.getUseLimit()!=null){
                if (totalPrice.compareTo(coupon.getUseLimit())>0){
                    totalPrice = totalPrice.subtract(coupon.getMinusNum());
                }
                if (totalPrice.compareTo(BigDecimal.ZERO)<0){
                    totalPrice = BigDecimal.ZERO;
                }
            }
        }
        return totalPrice;
    }
}
